# 测试修复后的流式输出

Write-Host "=== 测试修复后的流式输出 ===" -ForegroundColor Green

$baseUrl = "http://localhost:8082/api/ollama"

Write-Host "`n🔧 修复内容:" -ForegroundColor Yellow
Write-Host "1. SSE超时时间从60秒增加到5分钟" -ForegroundColor White
Write-Host "2. 添加ResponseBodyEmitter状态检查，避免向已完成的emitter发送数据" -ForegroundColor White
Write-Host "3. 优化错误处理逻辑，防止重复完成emitter" -ForegroundColor White

Write-Host "`n🧪 开始测试..." -ForegroundColor Cyan

try {
    $body = @{
        prompt = "你是谁？请简单介绍一下自己。"
    } | ConvertTo-Json

    Write-Host "`n发送测试请求..." -ForegroundColor Gray
    Write-Host "URL: $baseUrl/chat-stream" -ForegroundColor Cyan
    Write-Host "请求体: $body" -ForegroundColor Gray
    
    Write-Host "`n开始接收流式响应:" -ForegroundColor Yellow
    Write-Host "=" * 60 -ForegroundColor Gray

    # 使用Invoke-RestMethod测试流式输出
    $response = Invoke-RestMethod -Uri "$baseUrl/chat-stream" -Method POST -Body $body -ContentType "application/json" -TimeoutSec 300
    
    Write-Host $response -ForegroundColor Green
    
    Write-Host "`n=" * 60 -ForegroundColor Gray
    Write-Host "✅ 流式测试成功完成" -ForegroundColor Green

} catch {
    Write-Host "❌ 测试失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "详细错误信息:" -ForegroundColor Yellow
    Write-Host $_.Exception.ToString() -ForegroundColor Red
}

Write-Host "`n📋 测试结果分析:" -ForegroundColor Magenta
Write-Host "- 如果看到完整的AI回答，说明基本功能正常" -ForegroundColor White
Write-Host "- 如果没有看到'ResponseBodyEmitter has already completed'错误，说明修复成功" -ForegroundColor White
Write-Host "- 如果没有超时错误，说明超时时间设置合理" -ForegroundColor White

Write-Host "`n🌐 建议进一步测试:" -ForegroundColor Magenta
Write-Host "1. 打开浏览器访问: http://localhost:8082/stream.html" -ForegroundColor White
Write-Host "2. 选择'流式模式'进行可视化测试" -ForegroundColor White
Write-Host "3. 观察是否能看到逐字输出效果" -ForegroundColor White
Write-Host "4. 检查浏览器开发者工具的Network面板，查看SSE连接状态" -ForegroundColor White

Write-Host "`n💡 如果仍有问题:" -ForegroundColor Magenta
Write-Host "- 检查后端日志，确认没有'IllegalStateException'错误" -ForegroundColor White
Write-Host "- 确认Ollama服务正常运行" -ForegroundColor White
Write-Host "- 检查模型是否正确加载" -ForegroundColor White
